﻿Imports System.Data.SqlClient
Imports System.Security.Cryptography
Imports System.Text

Public Class Login

    Private cn As CnData
    Private da As SqlDataAdapter
    Private ds As DataSet
    Private cmd As SqlCommand
    Private dtr As SqlDataReader

    Public Sub New()
        InitializeComponent()
        ' db = New ADODB.Connection

    End Sub



    Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: cette ligne de code charge les données dans la table 'LaisserPasserDataSet1.Sites'. Vous pouvez la déplacer ou la supprimer selon vos besoins.
        Me.SitesTableAdapter.Fill(Me.LaisserPasserDS.Sites)

    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Application.Exit()
    End Sub

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click


        If String.IsNullOrEmpty(UsernameTextBox.Text) Or String.IsNullOrEmpty(PasswordTextBox.Text) Then
            MsgBox("Veuiller saisir le nom d'utilisateur et le mot de passe")
            Exit Sub
        End If

        Dim p As Accueil
        p = Me.Owner

        On Error GoTo err
        cn = New CnData
        cn.Open()
        Dim MD5Pass As String

        Using md5Hash As MD5 = MD5.Create()
            MD5Pass = GetMd5Hash(md5Hash, PasswordTextBox.Text.ToUpper)
            'MsgBox(MD5Pass)
        End Using


        'MsgBox(UsernameTextBox.Text.ToUpper)
        'MsgBox(MD5Pass)
        cmd = New SqlCommand("SELECT * FROM Utilisateurs WHERE Util='" & UsernameTextBox.Text.ToUpper & "' AND Mdp='" & MD5Pass & "'", cn.getCn)
        dtr = cmd.ExecuteReader

        If dtr.Read() Then
            'MsgBox(dtr.Item("Prenom").ToString)
            If checkSite.Checked Then
                p.DeclareUser(dtr.Item("Util").ToString, sites.SelectedValue, dtr.Item("Profil").ToString)
            Else
                p.DeclareUser(dtr.Item("Util").ToString, dtr.Item("Site").ToString, dtr.Item("Profil").ToString)
            End If

            dtr.Close()
            cn.Close()
            Me.Close()
            Exit Sub
        Else
            dtr.Close()
            MsgBox("Nom d'utilisateur ou mot de passe incorrect")
            cn.Close()
            Exit Sub
        End If

err:
        MsgBox("Une erreur s'est produite, impossible de continuer")
        cn.Close()
    End Sub

    Shared Function GetMd5Hash(ByVal md5Hash As MD5, ByVal input As String) As String

        ' Convert the input string to a byte array and compute the hash. 
        Dim data As Byte() = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input))

        ' Create a new Stringbuilder to collect the bytes 
        ' and create a string. 
        Dim sBuilder As New StringBuilder()

        ' Loop through each byte of the hashed data  
        ' and format each one as a hexadecimal string. 
        Dim i As Integer
        For i = 0 To data.Length - 1
            sBuilder.Append(data(i).ToString("x2"))
        Next i

        ' Return the hexadecimal string. 
        Return sBuilder.ToString()

    End Function


    Private Sub checkSite_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkSite.CheckedChanged
        If checkSite.Checked Then
            sites.Visible = True
        Else
            sites.Visible = False
        End If
    End Sub


End Class
